/**
 * Created with IntelliJ IDEA.
 * Description:
 */
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        int n = s.length();
        Map<Character, Integer> map = new HashMap<>();
        int maxFreq = 0;
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
            maxFreq = Math.max(maxFreq, map.get(c));
        }
        int ans = n;
        for (int i = 0; i < 4; i++) {
            char c = "ASDF".charAt(i);
            if (!map.containsKey(c)) {
                ans = 0;
                break;
            }
            int freq = map.get(c);
            ans = Math.min(ans, n - freq * 4);
            int l = 0, r = 0, cnt = 0;
            Map<Character, Integer> window = new HashMap<>();
            while (r < n) {
                char ch = s.charAt(r);
                window.put(ch, window.getOrDefault(ch, 0) + 1);
                if (window.get(ch) == freq) {
                    cnt++;
                }
                while (cnt == i + 1) {
                    ch = s.charAt(l);
                    window.put(ch, window.get(ch) - 1);
                    if (window.get(ch) == freq - 1) {
                        cnt--;
                    }
                    l++;
                }
                r++;
                if (cnt == i && r - l == freq * 4) {
                    ans = Math.min(ans, i * freq);
                }
            }
        }
        System.out.println(ans);
    }
}
